package cn.wyl.basic.util;

import cn.wyl.org.domain.Shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Component //@component是spring中的一个注解，它的作用就是实现bean的注入，交给spring管理
public class EmailUtil {

    //在静态变量上无法使用注解@Autowire注解（因为static修饰的类在程序启动就已经创建）。
    // 虽然注入时不会报错，当运行时期使用注入的对象时会报空指针异常
    @Autowired
    private  JavaMailSender javaMailSender;


    /**
     * 发送店铺管理员邮件，通知店铺注册成功，待激活
     * @param setTo 收件人邮箱
     * @param shop 待激活店铺
     * @throws MessagingException
     */

    public  void sendSucess(String setTo, Shop shop) throws MessagingException {
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        //设置发送方
        helper.setFrom("1572354484@qq.com");
        //设置主题
        helper.setSubject("店铺激活邮件");
        //设置内容
        helper.setText("<h1>你的店铺已经注册!!!</h1>" +
                "<img src='http://123.207.27.208"+shop.getLogo()+"' >" +
                "<a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        //收件人
        helper.setTo(setTo);
        //发送
        javaMailSender.send(mimeMessage);
    }


    public  void sendFail(String setTo, Shop shop) throws MessagingException {
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        //设置发送方
        helper.setFrom("1572354484@qq.com");
        //设置主题
        helper.setSubject("入驻失败邮件");
        //设置内容
        helper.setText("<h1>入驻失败!!!</h1>" +
                "<a href='http://localhost:8081/#/shopregister'>点击重新入驻</a>",true);
        //收件人
        helper.setTo(setTo);
        //发送
        javaMailSender.send(mimeMessage);
    }

}

